package deepbrain.simnetwork.structure;


/**
 * The Connection class represents a directed connection between two nodes.
 * 
 * The weight of a Connection can be mutable, but the nodes on two side of
 * Connection is immutable.
 * 
 * @author Li Yang
 * @create 2009-2-23
 */
public class Connection<WeightType> implements Weightable<WeightType> {

	public final int source;
	public final int destination;

	private WeightType weight;

	public Connection(int source, int destination) {
		this.source = source;
		this.destination = destination;
	}

	public Connection(int source, int destination, WeightType weight) {
		this(source, destination);
		this.weight = weight;
	}

	public WeightType getWeight() {
		return weight;
	}

	public void setWeight(WeightType weight) {
		this.weight = weight;
	}

	/**
	 * Setting hashCode Method for Being Contained in Hashing Container
	 */
	@Override
	public int hashCode() {
		int hashCode = source;
		hashCode = hashCode * 33 + destination;
		if (weight != null)
			hashCode = hashCode * 33 + weight.hashCode();
		return hashCode;
	}

	/**
	 * Setting equals Method for Being Contained in Hashing and Tree Container
	 */
	@SuppressWarnings("unchecked")
	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof Connection))
			return false;
		Connection<WeightType> object = (Connection<WeightType>) obj;
		if (weight == null) {
			if (object.weight != null)
				return false;
		} else {
			if (!weight.equals(object.weight))
				return false;
		}
		if (source != object.source)
			return false;
		if (destination != object.destination)
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Connection from " + source + " to " + destination
				+ " with Weight: " + weight;
	}

}
